package com.yifushidai.api;

import com.yifushidai.service.LockService;
import com.yifushidai.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "联网设备")
@RequestMapping("/api/lock")
public class LockController {

    @Autowired
    private LockService lockService;

    @ApiOperation(value = "手机号绑定联网设备接口", notes = "手机号绑定联网设备", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/bindMac")
    public R bindMac(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                     @ApiParam(value = "设备别名", required = true) @RequestParam String alias,
                     @ApiParam(value = "设备地址", required = true) @RequestParam String addr,
                     @ApiParam(value = "纬度") @RequestParam(required = false) Double lat,
                     @ApiParam(value = "经度") @RequestParam(required = false) Double lng,
                     @ApiParam(value = "蓝牙信息", required = true) @RequestParam String blueteeth,
                     @ApiParam(value = "用户头像图片文件流") @RequestBody(required = false) MultipartFile upLoadFile,
                     HttpServletRequest request) throws Exception {
        return lockService.saveMac(mac, alias, addr, lat, lng, blueteeth, upLoadFile, request);
    }

    @ApiOperation(value = "绑定家电信息接口", notes = "绑定家电功率（单位：千瓦）和电费单价（固定值：0.82元）信息", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/bindPower")
    public R bindPower(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                       @ApiParam(value = "电费单价", required = true) @RequestParam Double eprice,
                       @ApiParam(value = "家电功率", required = true) @RequestParam Double pow) throws Exception {
        return lockService.bandPower(mac, eprice, pow);
    }

    @PostMapping("/modImg")
    @ApiOperation(value = "修改房间图片接口", notes = "修改房间图片", produces = MediaType.APPLICATION_JSON_VALUE)
    public R modImg(@ApiParam(value = "头像文件流", required = true) @RequestBody MultipartFile upLoadFile,
                    @ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                    HttpServletRequest request) throws Exception {
        return lockService.modImg(upLoadFile, mac, request);
    }

    @PostMapping("/getMainLocks")
    @ApiOperation(value = "获取联网设备列表接口", notes = "获取当前登录用户联网设备列表", produces = MediaType.APPLICATION_JSON_VALUE)
    public R getMainLocks() throws Exception {
        return lockService.getMainLocks();
    }

    @PostMapping("/getPowerStatus")
    @ApiOperation(value = "判定用户是否拥有节电设备接口", notes = "判定当前登录用户是否拥有节电设备", produces = MediaType.APPLICATION_JSON_VALUE)
    public R hasPowerDevice(/*@ApiParam(value = "信鸽token", required = true) @RequestParam String xingeToken*/) throws Exception {
        return lockService.hasPowerDevice();
    }

    @PostMapping("/getBlueTeeth")
    @ApiOperation(value = "获取蓝牙信息接口", notes = "获取蓝牙信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public R getBluetooth(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac) throws Exception {
        return lockService.getBluetooth(mac);
    }

    @PostMapping("/powerofdelay")
    @ApiOperation(value = "设置延时断电时间接口", notes = "设置延时断电时间（单位：分钟）", produces = MediaType.APPLICATION_JSON_VALUE)
    public R powerofdelay(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                          @ApiParam(value = "延时断电时间（单位：分钟）", required = true) @RequestParam Integer powerOfDelay) throws Exception {
        return lockService.powerofdelay(mac, powerOfDelay);
    }

    @PostMapping("/del")
    @ApiOperation(value = "解绑联网设备接口", notes = "解绑联网设备（物理删除）", produces = MediaType.APPLICATION_JSON_VALUE)
    public R deleteByMac(@ApiParam(value = "联网盒mac", required = true) @RequestParam String mac,
                         HttpServletRequest request) throws Exception {
        return lockService.deleteByMac(mac, request);
    }

    @ApiOperation("查询管理员列表")
    @PostMapping("listMobile2")
    public R listMobile2() throws Exception {
        return lockService.listMobile2();
    }

    @ApiOperation("添加管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "param", value = "JSON参数", required = true),
            @ApiImplicitParam(paramType = "form", name = "mobile2", value = "备用手机号", required = true),
    })
    @PostMapping("addMobile2")
    public R addMobile2(@RequestParam String param, @RequestParam String mobile2) throws Exception {
        return lockService.addMobile2(param, mobile2);
    }

    @ApiOperation("删除管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "mobile2", value = "备用手机号", required = true),
    })
    @PostMapping("delMobile2")
    public R delMobile2(@RequestParam String mobile2) throws Exception {
        return lockService.delMobile2(mobile2);
    }

    @ApiOperation("查询联网设备（分页+搜索）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "alias", value = "设备别名", required = false),
            @ApiImplicitParam(paramType = "form", name = "offset", value = "偏移", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "form", name = "limit", value = "长度", required = true, dataType = "int"),
    })
    @PostMapping("page")
    public R page(String alias, @RequestParam int offset, @RequestParam int limit) throws Exception {
        return lockService.page(alias, offset, limit);
    }

    @ApiOperation("修改蓝牙")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "mac", value = "联网盒mac", required = true),
            @ApiImplicitParam(paramType = "form", name = "bluetooth", value = "蓝牙", required = true),
    })
    @PostMapping("modBluetooth")
    public R modBluetooth(@RequestParam String mac, @RequestParam String bluetooth) throws Exception {
        return lockService.modBluetooth(mac, bluetooth);
    }

    @ApiOperation("修改设备手机号（设备转让）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "mac", value = "联网设备MAC", required = true),
            @ApiImplicitParam(paramType = "form", name = "mobile", value = "手机号", required = true),
    })
    @PostMapping("modMobile")
    public R modMobile(@RequestParam String mac, @RequestParam String mobile) throws Exception {
        return lockService.modMobile(mac, mobile);
    }
}
